package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.exception.ResponseCode;
import com.nercel.dsj.gksales.exception.RestApiException;
import com.nercel.dsj.gksales.thirdparty.wxmp.Code2SessionResponse;
import com.nercel.dsj.gksales.thirdparty.wxservercode.entity.WxServerCodeAccessTokenEntity;
import com.nercel.dsj.gksales.thirdparty.wxservercode.service.WxServerCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sunlin
 * @date 2019-2-28
 */
@RestController
@Api(tags = "第三方认证")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ThirdAuthApiController extends BaseApiController {

    private final WxServerCodeService wxServerCodeService;

	@GetMapping(value = "/api/v1/third-auth/wxh5/login")
	@ApiOperation(value = "获取登录信息（公众号）")
	public Code2SessionResponse wxh5Login(
			@ApiParam(value = "CODE)") @RequestParam(required = false) String code
	) {
		Code2SessionResponse sessionResponse = new Code2SessionResponse();
		WxServerCodeAccessTokenEntity wxServerCodeAccessTokenEntity = wxServerCodeService.getAccessTokenInfoByCode(code);
		if(wxServerCodeAccessTokenEntity.getOpenId() == null){
			throw new RestApiException(ResponseCode.NO_SOCIAL_ACCOUNT);
		}
		sessionResponse.setOpenid(wxServerCodeAccessTokenEntity.getOpenId());
		return sessionResponse;
	}

}
